﻿Imports System.ComponentModel
Imports System.Collections.ObjectModel
Imports IETracker.UI.Process.PhoneClassLibrary.IETrackerServiceReference
Imports System.Net.NetworkInformation

Public Class MainViewModel
    Implements INotifyPropertyChanged
    Private client As New IETrackerServiceClient

    Public Sub New()
        Me.Items = New ObservableCollection(Of TransactionList)()
        Me.ItemsIncome = New ObservableCollection(Of TransactionList)()
    End Sub

    ''' <summary>
    ''' A collection for ItemViewModel objects.
    ''' </summary>
    Private _itemsIncome As ObservableCollection(Of TransactionList)
    Public Property ItemsIncome() As ObservableCollection(Of TransactionList)
        Get
            Return _itemsIncome
        End Get
        Set(ByVal value As ObservableCollection(Of TransactionList))
            _itemsIncome = value
        End Set
    End Property

    ''' <summary>
    ''' A collection for ItemViewModel objects.
    ''' </summary>
    Private _items As ObservableCollection(Of TransactionList)
    Public Property Items() As ObservableCollection(Of TransactionList)
        Get
            Return _items
        End Get
        Set(ByVal value As ObservableCollection(Of TransactionList))
            _items = value
        End Set
    End Property

    Private _sampleProperty As String = "Sample Runtime Property Value"
    ''' <summary>
    ''' Sample ViewModel property; this property is used in the view to display its value using a Binding
    ''' </summary>
    ''' <returns></returns>
    Public Property SampleProperty() As String
        Get
            Return _sampleProperty
        End Get
        Set(ByVal value As String)
            If Not value.Equals(_sampleProperty) Then
                _sampleProperty = value
                NotifyPropertyChanged("SampleProperty")
            End If
        End Set
    End Property

    Private _isDataLoaded As Boolean
    Public Property IsDataLoaded() As Boolean
        Get
            Return _isDataLoaded
        End Get
        Set(ByVal value As Boolean)
            _isDataLoaded = value
        End Set
    End Property

    Private _isInternetConnectionAvailable As Boolean
    Public Property isInternetConnectionAvailable() As Boolean
        Get
            Return _isInternetConnectionAvailable
        End Get
        Set(ByVal value As Boolean)
            _isInternetConnectionAvailable = value
        End Set
    End Property

    Private _isNotifiedIETrackerServiceUnavailable As Boolean
    Public Property isNotifiedIETrackerServiceUnavailable() As Boolean
        Get
            Return _isNotifiedIETrackerServiceUnavailable
        End Get
        Set(ByVal value As Boolean)
            _isNotifiedIETrackerServiceUnavailable = value
        End Set
    End Property

    ''' <summary>
    ''' Creates and adds a few ItemViewModel objects into the Items collection.
    ''' </summary>
    Public Sub LoadData()
        If InternetIsAvailable() Then
            AddHandler client.GetExpenseListCompleted, AddressOf client_GetExpenseListCompleted
            client.GetExpenseListAsync(DateTime.Now.AddDays(-14), 20)
            AddHandler client.GetIncomeListCompleted, AddressOf client_GetIncomeListCompleted
            client.GetIncomeListAsync(DateTime.Now.AddDays(-14), 20)
        End If
    End Sub

    Private Sub client_GetExpenseListCompleted(sender As Object, e As GetExpenseListCompletedEventArgs)
        If e.[Error] Is Nothing Then
            For Each item In e.Result
                Items.Add(item)
            Next
            Me.IsDataLoaded = True
            ' _transactionList = e.Result
            isNotifiedIETrackerServiceUnavailable = False

        Else
            If Not isNotifiedIETrackerServiceUnavailable Then
                MessageBox.Show("IE Tracker service is unavailable. Data will be temporarily saved on the phone.")
                isNotifiedIETrackerServiceUnavailable = True
            End If
        End If
    End Sub

    Private Sub client_GetIncomeListCompleted(sender As Object, e As GetIncomeListCompletedEventArgs)
        If e.[Error] Is Nothing Then
            For Each item In e.Result
                ItemsIncome.Add(item)
            Next
            Me.IsDataLoaded = True
            isNotifiedIETrackerServiceUnavailable = False
        Else
            If Not isNotifiedIETrackerServiceUnavailable Then
                MessageBox.Show("IE Tracker service is unavailable. Data will be temporarily saved on the phone.")
                isNotifiedIETrackerServiceUnavailable = True
            End If
        End If
    End Sub

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    Private Sub NotifyPropertyChanged(ByVal propertyName As String)
        Dim handler As PropertyChangedEventHandler = PropertyChangedEvent
        If Nothing IsNot handler Then
            handler(Me, New PropertyChangedEventArgs(propertyName))
        End If
    End Sub

    Function InternetIsAvailable() As Boolean
        Me.isInternetConnectionAvailable = NetworkInterface.GetIsNetworkAvailable()
        'Me.isInternetConnectionAvailable = False
        If Not Me.isInternetConnectionAvailable Then
            MessageBox.Show("No internet connection is available. Data will be temporarily saved on the phone.")
            Return False
        Else
            Return True
        End If
    End Function
End Class